package com.daisuxia.web.util;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.daisuxia.back.service.ITongBuService;


public class JmsConsumer {
	
/*	public static final String ADMIN = PropertiesFactory.getValue("admin"); 
	
	public static final String PWD = PropertiesFactory.getValue("pwd"); 
	*/
	public static   String ADRESS = "failover:(tcp://192.168.1.179:61616?wireFormat.maxInactivityDuration=0,tcp://192.168.1.225:61616?wireFormat.maxInactivityDuration=0)"; 
	
	public static  String QUEUENAME = "xjx"; 
	public static ExecutorService threadPool = Executors.newFixedThreadPool(100);
	
	public  static void getmessage(){
		    ConnectionFactory connectionFactory;
		    // Connection ：JMS 客户端到JMS Provider 的连接  
		    Connection connection = null;
		    // Session： 一个发送或接收消息的线程  
		    Session session;
		    // Destination ：消息的目的地;消息发送给谁.  
		    Destination destination;
		    // 消费者，消息接收者  
		    MessageConsumer consumer;
		    //connectionFactory = new ActiveMQConnectionFactory("admin", "admin", "tcp://192.168.3.130:61616");
		    connectionFactory =new ActiveMQConnectionFactory(JmsConsumer.ADRESS);
		    try {
		        // 构造从工厂得到连接对象  
		        connection = connectionFactory.createConnection();
		        // 启动  
		        connection.start();
		        // 获取操作连接  
		        //这个最好还是有事务
		        session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
		        // 获取session注意参数值xingbo.xu-queue是一个服务器的queue，须在在ActiveMq的console配置  
		        destination = session.createQueue(JmsConsumer.QUEUENAME);
		        consumer = session.createConsumer(destination);
		        consumer.setMessageListener(new MessageListener() {
		            @Override
		            public void onMessage(Message message) {
		                try {
		                   System.out.println(((ObjectMessage)message).getObject());
		                   Map<String, Object> map = new HashMap<String, Object>();
		                   JmsConsumer js = new JmsConsumer();
		                   js.send(map);
		                } catch (Exception e) {
		                	
		                }
		            }
		        });
		    } catch (Exception e) {
		        e.printStackTrace();
		    }

	}
	
	@Autowired
	ITongBuService tongBuService;
	
	/**
	 * 执行操作
	 * @param map
	 */
	public  void send( Map<String, Object> map) {
		tongBuService.tongbu(String.valueOf(map.get("repayId")), String.valueOf(map.get("type")));
	}

}
